// Deposit.java
// Represents a deposit ATM transaction

// subclass of Transaction
public class Deposit extends Transaction {
	private double amount; // amount to deposit
	private Keypad keypad; // reference to keypad
	private DepositSlot depositSlot; // reference to deposit slot
	private final static int CANCELED = 0; // constant for cancel option

	// Deposit constructor
	public Deposit(int userClientNumber, int userAccountNumber,
			Screen atmScreen, BankDatabase atmBankDatabase, Keypad atmKeypad,
			DepositSlot atmDepositSlot) {
		// initialize superclass variables
		super(userClientNumber, userAccountNumber, atmScreen, atmBankDatabase);

		// initialize references to keypad and deposit slot
		keypad = atmKeypad;
		depositSlot = atmDepositSlot;
	} // end Deposit constructor

	// perform transaction
	@Override // Overrides the superclass method: execute();
	public void execute() {
		BankDatabase bankDatabase = getBankDatabase(); // get reference
		Screen screen = getScreen(); // get reference

		amount = promptForDepositAmount(); // get deposit amount from user

		// check whether user entered a deposit amount or canceled
		if (amount != CANCELED) {
			// request deposit envelope containing specified amount
			screen.printTop();
			screen.printMessage("Please insert a deposit envelope containing $"
					+ amount);
			screen.printBot();

			// receive deposit envelope
			boolean envelopeReceived = depositSlot.isEnvelopeReceived();

			// check whether deposit envelope was received
			if (envelopeReceived) {
				screen.printTop();
				screen.printMessage("Your envelope has been " + "received.");
				screen.printBlank();
				screen.printMessage("NOTE: The money just deposited will not "
						+ "be available until we verify");
				screen.printMessage("the amount of any "
						+ "enclosed cash and your checks clear.");
				screen.printBot();

				// credit account to reflect the deposit
				bankDatabase.credit(getAccountNumber(), amount);
			} // end if
			else // deposit envelope not received
			{
				screen.printTop();
				screen.printMessage("You did not insert an "
						+ "envelope, so your transaction has been cancelled.");
				screen.printBot();
			} // end else
		} // end if
		else // user canceled instead of entering amount
		{
			screen.printTop();
			screen.printMessage("Canceling transaction...");
			screen.printBot();
		} // end else
	} // end method execute

	// prompt user to enter a deposit amount in cents
	private double promptForDepositAmount() {
		Screen screen = getScreen(); // get reference to screen

		// display the prompt
		screen.displayMessageLine("\nPlease enter a deposit amount in "
				+ "CENTS (or 0 to cancel): ");
		int input = keypad.getInput(); // receive input of deposit amount

		// check whether the user canceled or entered a valid amount
		if (input == CANCELED)
			return CANCELED;
		else {
			return (double) input / 100; // return dollar amount
		} // end else
	} // end method promptForDepositAmount
} // end class Deposit

/**************************************************************************
 * (C) Copyright 1992-2012 by Deitel & Associates, Inc. and * Pearson Education,
 * Inc. All Rights Reserved. * * DISCLAIMER: The authors and publisher of this
 * book have used their * best efforts in preparing the book. These efforts
 * include the * development, research, and testing of the theories and programs
 * * to determine their effectiveness. The authors and publisher make * no
 * warranty of any kind, expressed or implied, with regard to these * programs
 * or to the documentation contained in these books. The authors * and publisher
 * shall not be liable in any event for incidental or * consequential damages in
 * connection with, or arising out of, the * furnishing, performance, or use of
 * these programs. *
 *************************************************************************/
